package com.aiyige.page.my.message.notifymessage.interactmessage.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.message.notifymessage.interactmessage.model.InteractMessage;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.vondear.rxtools.RxTextTool;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InteractMessageAdapter extends BaseQuickAdapter<InteractMessage, BaseViewHolder> {


    public InteractMessageAdapter() {
        super(new LinkedList<InteractMessage>());
    }

    @Override
    protected int getDefItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case MessageUtil.MESSAGE_TYPE_COLLECTION:
                baseViewHolder = new CollectionViewHolder(getItemView(R.layout.collection_message_item, parent));
                break;
            case MessageUtil.MESSAGE_TYPE_COMMENT:
                baseViewHolder = new CommentViewHolder(getItemView(R.layout.comment_message_item, parent));
                break;
            case MessageUtil.MESSAGE_TYPE_LIKE:
                baseViewHolder = new LikeViewHolder(getItemView(R.layout.like_message_item, parent));
                break;
            case MessageUtil.MESSAGE_TYPE_ORDER:
                baseViewHolder = new OrderViewHolder(getItemView(R.layout.order_message_item, parent));
                break;
            default:
                baseViewHolder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewHolder;
    }

    @Override
    protected void convert(BaseViewHolder helper, InteractMessage item) {
        switch (item.getType()) {
            case MessageUtil.MESSAGE_TYPE_COLLECTION:
                ((CollectionViewHolder) helper).bindData(item);
                break;
            case MessageUtil.MESSAGE_TYPE_COMMENT:
                ((CommentViewHolder) helper).bindData(item);
                break;
            case MessageUtil.MESSAGE_TYPE_LIKE:
                ((LikeViewHolder) helper).bindData(item);
                break;
            case MessageUtil.MESSAGE_TYPE_ORDER:
                ((OrderViewHolder) helper).bindData(item);
                break;
        }
    }

    public static class OrderViewHolder extends BaseViewHolder {
        @BindView(R.id.otherHeadIv)
        ImageView otherHeadIv;
        @BindView(R.id.otherNameTv)
        TextView otherNameTv;
        @BindView(R.id.otherTimeTv)
        TextView otherTimeTv;
        @BindView(R.id.ordernoTv)
        TextView ordernoTv;
        @BindView(R.id.contactTv)
        TextView contactTv;
        @BindView(R.id.otherOrderMessageTv)
        TextView otherOrderMessageTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.mediaTitleTv)
        TextView mediaTitleTv;
        @BindView(R.id.mediaReleaseDateTv)
        TextView mediaReleaseDateTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.quantityTv)
        TextView quantityTv;
        @BindView(R.id.refundedLayout)
        View refundedLayout;
        @BindView(R.id.rejectRefundLayout)
        View rejectRefundLayout;
        @BindView(R.id.rejectReasonTv)
        TextView rejectReasonTv;

        public OrderViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.otherHeadIv);
            addOnClickListener(R.id.otherNameTv);
            addOnClickListener(R.id.orderLayout);
        }

        public void bindData(InteractMessage data) {
            otherNameTv.setText(data.getOtherName());
            Glide.with(itemView.getContext())
                    .load(data.getOtherAvatar())
                    .apply(RequestOptions.circleCropTransform())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_head))
                    .into(otherHeadIv);
            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            otherTimeTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd HH:mm:ss", data.getOtherTime()));
            mediaTitleTv.setText(data.getMediaTitle());
            ordernoTv.setText(String.valueOf(data.getOrderNo()));
            contactTv.setText(data.getContact());
            otherOrderMessageTv.setText(data.getOtherOrderMessage());
            mediaReleaseDateTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", data.getMediaReleaseDate()));
            priceTv.setText(StringUtils.getString(R.string.currency_symbols) + StringUtils.priceFormat(data.getPrice()));
            quantityTv.setText("x" + data.getQuantity());
            switch (data.getOrderStatus()) {
                case MessageUtil.ORDER_STATUS_REFUNDED:
                    refundedLayout.setVisibility(View.VISIBLE);
                    rejectRefundLayout.setVisibility(View.GONE);
                    break;
                case MessageUtil.ORDER_STATUS_REJECT_REFUND:
                    refundedLayout.setVisibility(View.GONE);
                    rejectRefundLayout.setVisibility(View.VISIBLE);
                    rejectReasonTv.setText(data.getRejectReason());
                    break;
                default:
                    refundedLayout.setVisibility(View.GONE);
                    rejectRefundLayout.setVisibility(View.GONE);
                    break;
            }
        }
    }


    public static class LikeViewHolder extends BaseViewHolder {
        @BindView(R.id.otherHeadIv)
        ImageView otherHeadIv;
        @BindView(R.id.otherNameTv)
        TextView otherNameTv;
        @BindView(R.id.otherTimeTv)
        TextView otherTimeTv;
        @BindView(R.id.otherLikeMessageTv)
        TextView otherLikeMessageTv;
        @BindView(R.id.myCommentTv)
        TextView myCommentTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.mediaReleaseDateTv)
        TextView mediaReleaseDateTv;
        @BindView(R.id.mediaTitleTv)
        TextView mediaTitleTv;
        @BindView(R.id.playIv)
        ImageView playIv;
        @BindView(R.id.myLayout)
        View myLayout;

        public LikeViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.otherHeadIv);
            addOnClickListener(R.id.otherNameTv);
        }

        public void bindData(InteractMessage data) {
            otherNameTv.setText(data.getOtherName());
            Glide.with(itemView.getContext())
                    .load(data.getOtherAvatar())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_head))
                    .apply(RequestOptions.circleCropTransform())
                    .into(otherHeadIv);

            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);

            otherTimeTv.setText(DateFormatUtil.getStandardDate(data.getOtherTime()));
            mediaTitleTv.setText(data.getMediaTitle());
            mediaReleaseDateTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", data.getMediaReleaseDate()));
            playIv.setVisibility(data.getTargetType() == MessageUtil.TARGET_TYPE_VIDEO || data.getTargetType() == MessageUtil.TARGET_TYPE_LEARN_VIDEO ? View.VISIBLE : View.INVISIBLE);
            switch (data.getTargetType()) {
                case MessageUtil.TARGET_TYPE_COMMENT:
                    myLayout.setVisibility(View.VISIBLE);
                    RxTextTool.getBuilder("")
                            .append(data.getMyName() + ": ").setForegroundColor(0xff909090).setProportion(1.1f)
                            .append(data.getMyComment()).setForegroundColor(0xff606060)
                            .into(myCommentTv);
                    otherLikeMessageTv.setText(R.string.other_like_comment_message);
                    break;
                default:
                    myLayout.setVisibility(View.GONE);
                    otherLikeMessageTv.setText(R.string.other_like_media_message);
                    break;
            }
        }
    }


    public static class CommentViewHolder extends BaseViewHolder {
        @BindView(R.id.otherHeadIv)
        ImageView otherHeadIv;
        @BindView(R.id.otherNameTv)
        TextView otherNameTv;
        @BindView(R.id.otherTimeTv)
        TextView otherTimeTv;
        @BindView(R.id.otherCommentMessageTv)
        TextView otherCommentMessageTv;
        @BindView(R.id.myCommentTv)
        TextView myCommentTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.mediaReleaseDateTv)
        TextView mediaReleaseDateTv;
        @BindView(R.id.mediaTitleTv)
        TextView mediaTitleTv;
        @BindView(R.id.playIv)
        ImageView playIv;
        @BindView(R.id.myLayout)
        View myLayout;

        public CommentViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.otherHeadIv);
            addOnClickListener(R.id.otherNameTv);
            addOnClickListener(R.id.replyBtn);
        }

        public void bindData(InteractMessage data) {
            otherNameTv.setText(data.getOtherName());
            Glide.with(itemView.getContext())
                    .load(data.getOtherAvatar())
                    .apply(RequestOptions.placeholderOf(android.R.color.white))
                    .apply(RequestOptions.circleCropTransform())
                    .into(otherHeadIv);

            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(android.R.color.white))
                    .into(coverIv);

            otherTimeTv.setText(DateFormatUtil.getStandardDate(data.getOtherTime()));
            mediaTitleTv.setText(data.getMediaTitle());
            mediaReleaseDateTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", data.getMediaReleaseDate()));
            playIv.setVisibility(data.getTargetType() == MessageUtil.TARGET_TYPE_VIDEO || data.getTargetType() == MessageUtil.TARGET_TYPE_LEARN_VIDEO ? View.VISIBLE : View.INVISIBLE);
            switch (data.getTargetType()) {
                case MessageUtil.TARGET_TYPE_COMMENT: {
                    myLayout.setVisibility(View.VISIBLE);
                    RxTextTool.getBuilder("")
                            .append(data.getMyName() + ": ").setForegroundColor(0xFF909090).setProportion(1.1f)
                            .append(data.getMyComment()).setForegroundColor(0xFF606060)
                            .into(myCommentTv);
                    RxTextTool.getBuilder("")
                            .append(StringUtils.getString(R.string.other_reply)).setForegroundColor(0xFF909090).setProportion(1.1f)
                            .append(data.getOtherComment()).setForegroundColor(0xFF333333)
                            .into(otherCommentMessageTv);
                    break;
                }
                default: {
                    myLayout.setVisibility(View.GONE);
                    RxTextTool.getBuilder("")
                            .append(StringUtils.getString(R.string.other_comment)).setForegroundColor(0xFF909090).setProportion(1.1f)
                            .append(data.getOtherComment()).setForegroundColor(0xFF333333)
                            .into(otherCommentMessageTv);
                    break;
                }
            }
        }
    }

    public static class CollectionViewHolder extends BaseViewHolder {
        @BindView(R.id.otherHeadIv)
        ImageView otherHeadIv;
        @BindView(R.id.otherNameTv)
        TextView otherNameTv;
        @BindView(R.id.otherTimeTv)
        TextView otherTimeTv;
        @BindView(R.id.otherFavouriteMessageTv)
        TextView otherFavouriteMessageTv;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.mediaReleaseDateTv)
        TextView mediaReleaseDataTv;
        @BindView(R.id.mediaTitleTv)
        TextView mediaTitleTv;
        @BindView(R.id.playIv)
        ImageView playIv;

        public CollectionViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.otherHeadIv);
            addOnClickListener(R.id.otherNameTv);
        }

        public void bindData(InteractMessage data) {
            otherNameTv.setText(data.getOtherName());
            Glide.with(itemView.getContext())
                    .load(data.getOtherAvatar())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_head))
                    .apply(RequestOptions.circleCropTransform())
                    .into(otherHeadIv);
            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            switch (data.getTargetType()) {
                case MessageUtil.TARGET_TYPE_SPECIAL_TOPIC:
                case MessageUtil.TARGET_TYPE_INFO:
                case MessageUtil.TARGET_TYPE_PHOTO:
                case MessageUtil.TARGET_TYPE_VIDEO:
                    otherFavouriteMessageTv.setText(R.string.collect_my_dynamic);
                    break;
                case MessageUtil.TARGET_TYPE_MAJOR_COURSE:
                case MessageUtil.TARGET_TYPE_LEARN_VIDEO:
                    otherFavouriteMessageTv.setText(R.string.collect_my_goods);
                    break;
                default:
                    otherFavouriteMessageTv.setText("");
                    break;
            }
            otherTimeTv.setText(DateFormatUtil.getStandardDate(data.getOtherTime()));
            mediaTitleTv.setText(data.getMediaTitle());
            mediaReleaseDataTv.setText(DateFormatUtil.formatMS("yyyy-MM-dd", data.getMediaReleaseDate()));
            playIv.setVisibility(data.getTargetType() == MessageUtil.TARGET_TYPE_VIDEO || data.getTargetType() == MessageUtil.TARGET_TYPE_LEARN_VIDEO ? View.VISIBLE : View.INVISIBLE);
        }
    }


}
